package cn.edu.nuc.thrid.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.nuc.thrid.dto.PageInfo;
import cn.edu.nuc.thrid.model.Goods;
import cn.edu.nuc.thrid.services.interfaces.GoodsService;

@Controller
@RequestMapping("/user")
public class UserGoodsController {

	@Autowired
	private GoodsService service;

	/**
	 * 列表
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/goodlist", method = RequestMethod.GET)
	public String goodlist(Model model) {

		List<Goods> list = service.goodlist();
		System.out.println(list);
		model.addAttribute("goodslist", list);
		return "user/userGoods";
	}

	/**
	 * 这部分代码跟goodscontroller的方法一样，冗余，以后避免
	 * @param mes
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/searchgood", method = RequestMethod.POST)
	public String search(String mes, Model model) {

		
		if (mes == null || "".equals(mes)) {

			return "redirect:/user/userpagelist";
		}
		PageInfo pageInfo = new PageInfo();
		List<Goods> list = service.searchgood(mes);
		pageInfo.setList(list);
		// System.out.println(list);
		model.addAttribute("p", pageInfo);
		return "user/userGoods";
	}

	/**
	 * 跳转
	 * 
	 * @return
	 */
	@RequestMapping(value = "/goodmes", method = RequestMethod.GET)
	public String goodMes(String zybgoodid, Model model) {

		// System.out.println(zybgoodid);

		Goods goods = service.initgood(Integer.parseInt(zybgoodid));
		model.addAttribute("goods", goods);
		return "user/goodMes";
	}
	/**
	 * 用户分页
	 * @param current
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/userpagelist",method=RequestMethod.GET)
	public String pagelist(@RequestParam(name="current",defaultValue="1")Integer current,Model model){
		
		PageInfo pageInfo = service.pagelist(current);

		model.addAttribute("p", pageInfo);
		return "user/userGoods";
	}
	
	
}
